package com.zsj.comminlibrary.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import androidx.annotation.Nullable;


/**
 * Created by 杰 on 2018/6/19.
 */

public class ProgressNoTouchView extends RelativeLayout {

    int mRadus = 35;
    /*圆角的半径，依次为左上角xy半径，右上角，右下角，左下角*/
    private float[] rids = {mRadus, mRadus, mRadus, mRadus, mRadus, mRadus, mRadus, mRadus,};

    private Context mContext;
    private int mMeasuredWidth;
    private int selectPos = 0;
    private ImageView mMasking;
    private float mDownRawX;
    public onProgressChange mOnProgressChange;
    private ImageView mView;
    private int mPostColor = 0xffFFB83C;
    private int backColor = 0xff323232;

    public ProgressNoTouchView(Context context) {
        super(context);
        initView(context);
    }

    public ProgressNoTouchView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public ProgressNoTouchView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    public void setBackColor(int backColor) {
        this.backColor = backColor;
        mMasking.setBackgroundColor(backColor);
    }

    public void setSelectPos(int selectPos) {
        this.selectPos = selectPos;
        setPos();
    }

    public void setOnProgressChange(onProgressChange onProgressChange) {
        mOnProgressChange = onProgressChange;
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mMeasuredWidth = getMeasuredWidth();
//        setPos();
    }

    private void initView(Context context) {
        mContext = context;
        mView = new ImageView(context);
        mView.setBackgroundColor(mPostColor);
        mView.setScaleType(ImageView.ScaleType.FIT_XY);
        addView(mView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        mMasking = new ImageView(mContext);
        mMasking.setBackgroundColor(backColor);
        int width = (int) ((100 - selectPos) / 100f * mMeasuredWidth);
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        addView(mMasking, params);
        setFocusableInTouchMode(true);
        mView.setVisibility(INVISIBLE);
    }

    public void setPostColor(int res) {
        mPostColor = res;
        mView.setBackgroundColor(mPostColor);
    }

    public void setNullPregressRes(int res) {
        mMasking.setBackgroundResource(res);
    }

    private int foucePos = -1;

    @Override
    protected void onFocusChanged(boolean gainFocus, int direction, @Nullable Rect previouslyFocusedRect) {
        super.onFocusChanged(gainFocus, direction, previouslyFocusedRect);
        if (!gainFocus) {
            if (mOnProgressChange != null && foucePos != selectPos) {
                mOnProgressChange.onChangeFinish(selectPos);
            }
        } else {
            foucePos = selectPos;
        }
    }


    public void changePos(int movePos) {
        selectPos = selectPos + movePos;
        selectPos = selectPos > 100 ? 100 : (selectPos < 0 ? 0 : selectPos);
        if (mMasking == null) {
            mMasking = new ImageView(mContext);
            mMasking.setBackgroundColor(backColor);
            int width = (int) ((100 - selectPos) / 100f * mMeasuredWidth);
            addView(mMasking, width, LayoutParams.MATCH_PARENT);
        } else {
            setPos();
        }
        if (mOnProgressChange != null) {
            mOnProgressChange.onChange(selectPos);
        }
    }

    private synchronized void setPos() {
        if (getMeasuredWidth() == 0) {
            return;
        }
        int width = 0;
        width = (int) ((100 - selectPos) / 100.0 * getMeasuredWidth());
        LayoutParams params = (LayoutParams) mMasking.getLayoutParams();
        params.width = width;
        mMasking.setLayoutParams(params);
        mView.setVisibility(VISIBLE);
//        invalidate();
    }

    public interface onProgressChange {
        void onChangeFinish(int pos);

        void onChange(int pos);
    }

    protected void onDraw(Canvas canvas) {
        Path path = new Path();
        int w = this.getWidth();
        int h = this.getHeight();
        /*向路径中添加圆角矩形。radii数组定义圆角矩形的四个圆角的x,y半径。radii长度必须为8*/
        path.addRoundRect(new RectF(0, 0, w, h), rids, Path.Direction.CW);
        canvas.clipPath(path);
        super.onDraw(canvas);
    }

    public int getSelectPos() {
        return selectPos;
    }
}
